import store from "@/store/store";
import Compressor from "compressorjs";
const { programState } = store;
async function compressImage(file: File | Blob, maxSize: number = 1) {
  if (programState.platform === "H5") {
    return await _h5Compress(file, maxSize);
  } else {
  }
}
async function _h5Compress(file: File | Blob, maxSize: number): Promise<string> {
  return await new Promise((resolve, reject) => {
    new Compressor(file, {
      quality: 0.2,
      convertSize: maxSize * 1024 * 1024,
      checkOrientation: true,
      success: (result: any) => {
        if (result.size > maxSize * 1024 * 1024) {
          reject(new Error(`1001/图片尺寸过大，请控制在${maxSize}M以内`));
        } else {
          const filePath = URL.createObjectURL(result);
          resolve(filePath);
        }
      },
      error: () => reject(new Error(`1001/图片压缩失败，请重试`)),
    });
  });
}

export { compressImage };
